package com.qing.study.serviceImpl;

import com.qing.study.comm.Result;
import com.qing.study.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class SendMailServiceImpl implements SendMailService {
    @Value("${spring.mail.username}")
    private String emailFrom;

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public Result sendMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailFrom);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
            javaMailSender.send(message);
            return Result.ok("邮件发送成功！");
        } catch (MailException e) {
            return Result.error(500,"邮件发送失败！");
        }

    }

    @Override
    public Result sendMail(String to, String subject, String content, String filePath)  {
        try {
            //发送一个带附件的邮箱
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //创建一个MimeMessageHelper对象 配置邮件信息 设置true支持多附件
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            String fileType = getFileType(filePath);

            mimeMessageHelper.addAttachment("附件."+fileType, new File(filePath));
            mimeMessageHelper.setSubject(subject);
            //设置邮件内容 true表示支持html格式
            mimeMessageHelper.setText(content, true);
            mimeMessageHelper.setFrom(emailFrom);
            mimeMessageHelper.setTo(to);

            javaMailSender.send(mimeMessage);
            return Result.ok("邮件发送成功！");
        } catch (MessagingException e) {
            return Result.error(500,"邮件发送失败！");
        }
    }

    public static String getFileType(String filePath) {
        // 获取文件的扩展名
        String fileName = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length());
        if (filePath.contains("\\")) {
            fileName = filePath.substring(filePath.lastIndexOf('\\') + 1, filePath.length());
        }
        int lastIndexOfDot = fileName.lastIndexOf('.');
        if (lastIndexOfDot == -1) {
            return "未知类型"; // 没有扩展名
        }
        String extension = fileName.substring(lastIndexOfDot + 1);

        // 基于扩展名判断文件类型（这里只是简单示例）
        switch (extension.toLowerCase()) {
            case "jpg":
                return "jpg";
            case "jpeg":
                return "jpeg";
            case "png":
                return "png";
            case "txt":
                return "txt";
            case "doc":
                return "doc";
            case "md":
                return "md";
            // 添加更多的case来支持其他文件类型
            default:
                return "未知类型";
        }
    }
}
